﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BiffenModel;
using System.Net;

namespace BiffenServiceLibary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.

    public class RemoteBookingSystem : IRemoteBookingSystem
    {
        private const string ConnString = @"Data Source=balder.ucn.dk;Initial Catalog=dm72_3;Persist Security Info=True;User ID=dm72_3;Password=MaaGodt";

        public RemoteBookingSystem()
        {
            Console.WriteLine("Welcome");



        }

        #region IBiffenDalConnection
        private static IBiffenDal Connection()
        {
            IBiffenDal dal = new BiffenDal();
            dal.OpenConnection(ConnString);
            return dal;
        }
        #endregion


        #region Person



        public void NewCustomer(string fname, string lname, string street, string streetNo, int zipCode, string phone, string email, DateTime birthDate)
        {
            IBiffenDal iDal = Connection();
            iDal.InsertCustomer(fname, lname, street, streetNo, zipCode, phone, birthDate, email);
            iDal.CloseConnection();
        }

        public void NewEmployee(string fname, string lname, string street, string streetNo, int zipCode, string phone, string email, string password, string username)
        {
            IBiffenDal iDal = Connection();
            iDal.InsertEmployee(fname, lname, street, streetNo, zipCode, phone, username, password, email);
            iDal.CloseConnection();
        }

        public void DeleteCustomer(int cusId)
        {
            IBiffenDal iDal = Connection();
            int persID = iDal.GetPersonIdFromCusID(cusId);
            if (persID != -1)
            {
                iDal.DeleteCustomer(cusId, persID);
            }
            else
            {
                Console.WriteLine("Fejl i DB");
            }
            iDal.CloseConnection();
        }

        public void DeleteEmployee(int empId)
        {
            IBiffenDal iDal = Connection();
            int persID = iDal.GetPersonIdFromEmpID(empId);
            if (persID != -1)
            {
                iDal.DeleteEmployee(empId, persID);
            }
            else
            {
                Console.WriteLine("Fejl i DB");
            }
            iDal.CloseConnection();
        }

        public void EditCustomer(int id, string fname, string lname, string street, string streetNo, int zipCode, string phone, DateTime birthDate, string email)
        {
            IBiffenDal iDal = Connection();
            iDal.EditCustomer(id, fname, lname, street, streetNo, zipCode, phone, birthDate, email);
            iDal.CloseConnection();
        }

        public void EditEmployee(int id, string fname, string lname, string street, string streetNo, int zipCode, string phone, string userName, string password, string email)
        {
            IBiffenDal iDal = Connection();
            iDal.EditEmployee(id, fname, lname, street, streetNo, zipCode, phone, userName, password, email);
            iDal.CloseConnection();
        }

        public List<Customer> AllCustomers()
        {
            List<Customer> lCus = new List<Customer>();
            IBiffenDal iDal = Connection();
            lCus = iDal.AllCustomers();
            return lCus;
        }
        public List<Employee> AllEmployee()
        {
            List<Employee> lCus = new List<Employee>();
            IBiffenDal iDal = Connection();
            lCus = iDal.AllEmployee();
            return lCus;
        }

        public Customer GetOneCustomer(int id)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetOneCustomer(id);
        }


        public Employee GetOneEmployee(int id)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetOneEmployee(id);
        }

        public Employee GetEmployeeUserName(string userName)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetEmployeeUserName(userName);
        }

        public int GetCustomerIDByPhone(string phone)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetPersonIdFromCusPhone(phone);
        }

        #endregion

        #region Movie
        public string getMovie(string title, int year)
        {
            string url;
            if(year== 0)
                url = "http://www.imdbapi.com/?t=" + title;
            else
                url = "http://www.imdbapi.com/?t=" + title + "&y=" + year;
            
            string result;

            WebClient client = new WebClient();
            result = client.DownloadString(url);

            return result;
        }

        public void newMovie(string title, string director, string description, string genre, string timeLength, string picture, List<string> artists)
        {
            try
            {
                IBiffenDal iDal = Connection();
                int movieID = iDal.InsertMovie(title, director, description, genre, timeLength, picture);
                if (artists.Count > 0)
                {
                    iDal.addArtistsToMovie(movieID, artists);
                }
                iDal.CloseConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine("Fejl in newMovie: " + e);
            }
        }

        public List<Movie> listMoviesByState(bool state)
        {
                List<Movie> _movies = new List<Movie>();
                IBiffenDal iDal = Connection();
                _movies = iDal.AllStateMovies(state);
                return _movies;
        }

        public List<Movie> allMovies()
        {
            List<Movie> _movies = new List<Movie>();
            IBiffenDal iDal = Connection();
            _movies = iDal.AllMovies();
            return _movies;
        }
        public Movie getOneMovie(int movID)
        {
            Movie _movie = new Movie();
            IBiffenDal iDal = Connection();
            _movie = iDal.getOneMovie(movID);
            return _movie;
        }

        #endregion

        #region CinemaHall
        public void NewCinemaHall(int showID)
        {
            IBiffenDal iDal = Connection();
            iDal.NewCinemaHall(showID);
            iDal.CloseConnection();
        }

        public CinemaHall GetOneCinemaHall(int cinemaID)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetOneCinemaHall(cinemaID);
        }

        public void DeleteCinemaHall(int cinemaID)
        {
            IBiffenDal iDal = Connection();
            iDal.DeleteCinemaHall(cinemaID);
            iDal.CloseConnection();
        }

        public void UpdateCinemaHall(int cinemaID, int showID)
        {
            IBiffenDal iDal = Connection();
            iDal.UpdateCinemaHall(cinemaID, showID);
            iDal.CloseConnection();
        }

        public List<CinemaHall> AllCinemas()
        {
            IBiffenDal iDal = Connection();
            return iDal.AllCinemas();
        }
        #endregion

        #region Show
        public void NewShow(DateTime date, int movID)
        {
            IBiffenDal iDal = Connection();
            iDal.NewShow(date, movID);
            iDal.CloseConnection();
        }

        public Show GetOneShow(int showID)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetOneShow(showID);
        }

        public void DeleteShow(int showID)
        {
            IBiffenDal iDal = Connection();
            iDal.DeleteShow(showID);
            iDal.CloseConnection();
        }

        public void UpdateShow(int showID, DateTime date, int movieID)
        {
            IBiffenDal iDal = Connection();
            iDal.UpdateShow(showID, date, movieID);
            iDal.CloseConnection();
        }

        public List<Show> AllShows()
        {
            IBiffenDal iDal = Connection();
            return iDal.AllShows();
        }
        #endregion

        #region Booking
        public void NewBooking(DateTime date, int cinID, int showID, int perID, int seatNo)
        {
            IBiffenDal iDal = Connection();
            iDal.NewBooking(date, cinID, showID, perID, seatNo);
            iDal.CloseConnection();
        }

        public Booking GetOneBooking(int bookID)
        {
            IBiffenDal iDal = Connection();
            return iDal.GetOneBooking(bookID);
        }

        public void UpdateBooking(int bookID, DateTime date, int cinID, int showID, int perID, int seatNo)
        {
            IBiffenDal iDal = Connection();
            iDal.UpdateBooking(bookID, date, cinID, showID, perID, seatNo);
            iDal.CloseConnection();
        }

        public void DeleteBooking(int bookID)
        {
            IBiffenDal iDal = Connection();
            iDal.DeleteBooking(bookID);
            iDal.CloseConnection();
        }

        public List<Booking> AllBookings()
        {
            IBiffenDal iDal = Connection();
            return iDal.AllBookings();
        }
        #endregion


    }

}
